﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Assessment.Management.SharedComponent.Params.MenuManagers;
using Assessment.Management.SharedComponent.Constants;
using Assessment.Management.BIZ;
using Assessment.Management.SharedComponent.Entities;

namespace Assessment.Management.UI.Admin
{
    public partial class Dashboard : System.Web.UI.Page
    {
        #region events
        protected void Page_Load(object sender, EventArgs e)
        {
             AccountParam param = GetAccount();
             LoadMenuManager(param.Account.Permission);
        }

        protected void rptParentMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField hd = (HiddenField)e.Item.FindControl("hdParentID");
            Repeater rptChild = (Repeater)e.Item.FindControl("rptChildMenu");
            MenuManagersParam param = new MenuManagersParam(FunctionType.GetChildMenu);
            param.MenuManagers = new MenuManager() { MenuParentID = int.Parse(hd.Value) };
            MainController.Provider.Execute(param);
            rptChild.DataSource = param.listMenuManagers;
            rptChild.DataBind();
        }

        protected void rptParentMenuV_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField hd = (HiddenField)e.Item.FindControl("hdParentIDV");
            Repeater rptChild = (Repeater)e.Item.FindControl("rptChildMenuV");
            MenuManagersParam param = new MenuManagersParam(FunctionType.GetChildMenu);
            param.MenuManagers = new MenuManager() { MenuParentID = int.Parse(hd.Value) };
            MainController.Provider.Execute(param);
            rptChild.DataSource = param.listMenuManagers;
            rptChild.DataBind();
        }

        protected void lbtLogout_Click(object sender, EventArgs e)
        {
            //HttpCookie username = Request.Cookies["username"];
            //username.Expires = DateTime.Now;
            //Response.Cookies.Add(username);
            //Response.Redirect("Login.aspx");
        }

        #endregion

        #region methods
        private void LoadMenuManager(int permission)
        {
            MenuManagersParam param = new MenuManagersParam(FunctionType.GetMenuManagers);
            param.MenuManagers = new MenuManager() { Permission = permission };
            MainController.Provider.Execute(param);
            rptParentMenuV.DataSource = rptParentMenu.DataSource = param.listMenuManagers;
            rptParentMenu.DataBind();
            rptParentMenuV.DataBind();
        }

        private AccountParam GetAccount()
        {
            //TODO: Edit account
            //string user = Request.Cookies["username"].Value;
            //Account acc = db.Accounts.SingleOrDefault(a => a.Username.ToString() == user);
            //if (acc == null)
            //{
            //    Response.Redirect("Login.aspx");
            //}
            AccountParam param = new AccountParam(FunctionType.GetAccount);
            param.Account = new Account(){ Username = "bangtv91" };
            MainController.Provider.Execute(param);
            if (param.Account == null)
            {
                Response.Redirect("Login.aspx");
            }
            lblUsername.Text = param.Account.Username;
            return param;
        }
        #endregion
    }
}